var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
	navTab: [], 
    currentTab: 20,
	nowList:[],
	leftList2: [],
	rightList2: [],
	leftHight2: 0,
	rightHight2: 0,
	
	start:0,
	page: 1,
	size: 6,
	allNum: 0,
	isEnd: false
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	this.proNum(this.data.currentTab);
	this.firstClass();
	this.getData(this.data.leftHight2,
		this.data.rightHight2,
		this.data.leftList2,
		this.data.rightList2,
		this.data.start,
		this.data.currentTab);   //
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	//获取更多
	this.data.start = this.data.page * this.data.size;
	this.getData(this.data.leftHight2,
				this.data.rightHight2,
				this.data.leftList2,
				this.data.rightList2,
				this.data.start,
				this.data.currentTab);
	
	this.data.page += 1;
	if(this.data.allNum < this.data.page * this.data.size){
		this.setData({
		isEnd: true
		})
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //产品数目
  proNum:function(cid){
  	  var that = this;
  	  var url_ = getApp().globalData.url_;
  	  // console.log(cid)
  	  // console.log(scid)
  	  wx.request({
  	    url:  url_ + '/api/product/total?cid='+ cid,
  	    method: "GET",
  	    dataType: "json",
  	    success: function(res){
  			that.setData({
  			  allNum: res.data.data.total,
  			})
  	    },
  	    fail:function(e){
  	      console.log("请求失败");
  	    },
  	  })
  },
  
  currentTab: function (e) {
    if (this.data.currentTab == e.currentTarget.dataset.id){
      return;
    }
    this.setData({
		currentTab: e.currentTarget.dataset.id,
		leftHight2: 0,
		rightHight2: 0,
		leftList2: [],
		rightList2: [],
		start: 0,
		page: 1,
		size: 6,
		isEnd: false
	 })
    this.getData(this.data.leftHight2,
	this.data.rightHight2,
	this.data.leftList2,
	this.data.rightList2,
	this.data.start,
	this.data.currentTab);
  },
  
  //获取列表
  getData:function(lastLH,lastRH,lastLeft,lastRight,start,cid){
	var that = this;
	var url_ = getApp().globalData.url_;
    if (this.data.isEnd){
      return
    }
	wx.request({
	  url:  url_ + '/api/product?start='+ start +'&count='+ that.data.size +'&cid=' + cid,
	  method: "GET",
	  dataType: "json",
	  success: function(res){
		//畅销商品
		var allData2 = res.data.data;
		//定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
		var leftH2 = lastLH;
		var rightH2 = lastRH;
		var leftData2 = [];
		var rightData2 = [];
		var CoverWidth2 = 0;
		var CoverHeight2 = 0;
		
		for (let j = 0; j < allData2.length; j++) {
			var img = url_ + allData2[j].cover_image;
			allData2[j].cover_image = img;
			CoverWidth2 = parseInt(allData2[j].imagewidth)
			CoverHeight2 = parseInt(allData2[j].imageheight);
			var currentItemHeight2 = parseInt((CoverHeight2 * 375) /CoverWidth2);
			if (leftH2 == rightH2 || leftH2 < rightH2) {//判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
				leftData2.push(allData2[j]);
				leftH2 += currentItemHeight2;
				} else {
					rightData2.push(allData2[j]);
					rightH2 += currentItemHeight2;
				}
			}
			//新旧数据合并
			var lastLeftData = lastLeft;
			var lastRightData = lastRight;
			lastLeftData.push(...leftData2);
			lastRightData.push(...rightData2);
			//畅销商品
			that.setData({
				productList: res.data.data,
				leftHight2: leftH2,
				rightHight2: rightH2,
				leftList2: lastLeftData,
				rightList2: lastRightData,
			})
	  },
	  fail:function(e){
	    console.log("请求失败");
	  },
	})
  },
 //左边右边列表
  
  //产品中心头部一级分类
  firstClass:function(){
	  var that = this;
	  var url_ = getApp().globalData.url_;
	  wx.request({
	    url:  url_ + '/api/product/category',
	    method: "GET",
	    dataType: "json",
	    success: function(res){
			// console.log(res.data.data.first);
			that.setData({
			  navTab: res.data.data.first,
			})
	    },
	    fail:function(e){
	      console.log("请求失败");
	    },
	  })
  },
  proGoDetail:function(e){
  console.log(e.currentTarget.dataset.id)
  	wx.navigateTo({
  		url: "../product/productDetails?id=" + e.currentTarget.dataset.id
  	})
  }
	  
  
})